package com.xbongbong.parent.abstracts;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataGetDTO;
import com.xbongbong.paas.pojo.dto.ParseSingleRowDataDTO;
import com.xbongbong.paas.pojo.vo.LinkResultDataVO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.parent.interfaces.ParentBusinessDetailService;
import com.xbongbong.parent.parse.data.FormDataDetailAnalysisDataHelp;
import com.xbongbong.parent.parse.data.FormDataLinkDataGetServiceImpl;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2021/12/30 14:22
 */
@Component
public abstract class AbstractParentBusinessDetailService<T, D> implements ParentBusinessDetailService<T, D> {



    @Resource
    private FormDataLinkDataGetServiceImpl formDataLinkDataGetServiceImpl;
    @Resource
    private FormDataDetailAnalysisDataHelp formDataDetailAnalysisDataHelp;

    /**
     * 解析单条数据，不包含子表单和关联数据
     * @param paasFormDataEntityExt
     * @param handlerExplainDTO
     * @param handlerExplainVO
     * @param list
     * @param explainMap
     * @return
     * @throws XbbException
     */
    protected ParseSingleRowDataVO commonAnalysisData(PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO, List<FieldAttrEntity> list, Map<String, FieldAttrEntity> explainMap) throws XbbException{
        JSONObject data = paasFormDataEntityExt.getData();
        String creatorId = paasFormDataEntityExt.getCreatorId();
        List<FieldAttrEntity> explainList = handlerExplainVO.getHeadList();
        ExplainUtil.getExplainMap(JSON.toJSONString(explainList), null);

        // 设置系统字段
        // 设置创建者
        data.put(FieldTypeEnum.ADDTIME.getAlias(), paasFormDataEntityExt.getAddTime());
        data.put(FieldTypeEnum.UPDATETIME.getAlias(), paasFormDataEntityExt.getUpdateTime());
        data.put(FieldTypeEnum.SERIALNO.getAlias(), paasFormDataEntityExt.getSerialNo());
        data.put(FieldTypeEnum.CREATORID.getAlias(), creatorId);
        data.put(FieldTypeEnum.DEPARTMENTID.getAlias(), paasFormDataEntityExt.getDepartmentId());
        data.put(FieldTypeEnum.OWNERID.getAlias(), handlerExplainVO.getOwnerId());
        data.put(FieldTypeEnum.COUSERID.getAlias(), handlerExplainVO.getCoUserId());
        //  获取用户和部门容器 解析paas的特殊数据 end
        ParseSingleRowDataDTO parseSingleRowDataDTO = new ParseSingleRowDataDTO();
        BeanUtil.copyProperties(handlerExplainDTO, parseSingleRowDataDTO);
        parseSingleRowDataDTO.setDataJsonObject(data);
        parseSingleRowDataDTO.setExplainMap(explainMap);
        parseSingleRowDataDTO.setUserMap(handlerExplainVO.getUserMap());
        parseSingleRowDataDTO.setDepartmentMap(handlerExplainVO.getDepartmentMap());
        // 解析数据
        ParseSingleRowDataVO parseSingleRowDataVO = formDataDetailAnalysisDataHelp.parseSingleRowData(parseSingleRowDataDTO);
        if ( Objects.isNull(parseSingleRowDataVO) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.PARSE_SINGLE_ROW_DATA_FAILED);
        }

        return parseSingleRowDataVO;
    }

    /**
     * 解析所有的子表单外数据，不包含子表单
     * @param paasFormDataEntityExt
     * @param handlerExplainDTO
     * @param handlerExplainVO
     * @param list
     * @param explainMap
     * @return
     * @throws XbbException
     */
    public JSONObject commonAnalysisAllData(PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO, List<FieldAttrEntity> list, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        ParseSingleRowDataVO parseSingleRowDataVO = commonAnalysisData(paasFormDataEntityExt, handlerExplainDTO, handlerExplainVO, list, explainMap);
        JSONObject data = parseSingleRowDataVO.getResult();
        Set<Object> linkDataSet = new HashSet<>();
        LinkResultDataVO linkResultDataVO = parseSingleRowDataVO.getLinkResultDataVO();
        if ( !linkResultDataVO.getLinkDataSet().isEmpty() ) {
            linkResultDataVO.getLinkDataSet().forEach((item -> {
                linkDataSet.add(item);
            }));
        }
        linkResultDataVO = linkResultDataVO == null ? new LinkResultDataVO() : linkResultDataVO;
        linkResultDataVO.setUserMap(handlerExplainVO.getUserMap());
        linkResultDataVO.setDepartmentMap(handlerExplainVO.getDepartmentMap());
        FormDataGetDTO formDataGetDTO = new FormDataGetDTO();
        BeanUtil.copyProperties(handlerExplainDTO, formDataGetDTO);
        if ( !linkResultDataVO.getLinkData().isEmpty() && !linkResultDataVO.getLinkDataSet().isEmpty() ) {
            linkResultDataVO.setData(data);
            linkResultDataVO.setExplainMap(explainMap);
            linkResultDataVO.setExplainList(list);
            data = formDataLinkDataGetServiceImpl.formDataLinkDataGet(formDataGetDTO, linkResultDataVO);
        }
        return data;
    }


}
